﻿using UnityEngine;
using UnityEngine.UI;
using UnityException = UnityEngine.UnityException;

public static class ListViewUtil
{

#if UNITY_EDITOR
    public static void CheckConfig(ScrollRect scrollRect)
    {
        var content = scrollRect.content;
        if (Mathf.Abs(content.pivot.y - 1) >= 0.001f)
        {
            throw new UnityException($"{scrollRect.name}'s ScrollRect.content.pivot.y must be 1");
        }
        var sizeFitter = content.GetComponent<ContentSizeFitter>();
        if (null == sizeFitter)
        {
            throw new UnityException($"{scrollRect.name}'s ScrollRect.content no ContentSizeFitter");
        }

        if (sizeFitter.verticalFit == ContentSizeFitter.FitMode.Unconstrained)
        {
            throw new UnityException($"{scrollRect.name}'s ScrollRect.content -> ContentSizeFitter.verticalFit must not be Unconstrained");
        }
        
        if (scrollRect.horizontal)
        {
            var horiLayoutGroup = content.GetComponent<HorizontalLayoutGroup>();
            if (null == horiLayoutGroup)
            {
                throw new UnityException($"{scrollRect.name}'s ScrollRect.content no HorizontalLayoutGroup");
            }
        }
        else if (scrollRect.vertical)
        {
            var verLayoutGroup = content.GetComponent<VerticalLayoutGroup>();
            if (null == verLayoutGroup)
            {
                throw new UnityException($"{scrollRect.name}'s ScrollRect.content no VerticalLayoutGroup");
            }
        }
        else
        {
            throw new UnityException($"{scrollRect.name}'s ScrollRect either vertical or horizontal must be checked");
        }
    }
#endif
    
    ///自己左下角相对父节点左下角的坐标 (x方向: 左为负, 右为正; y方向: 下为负, 上为正)
    public static Vector2 GetLeftBottomCornerLPos(RectTransform rtf)
    {
        Rect rect = rtf.rect;
        Vector2 size = rect.size;
        
        var parentRtf = (RectTransform)rtf.parent;
        Vector2 parentSize = parentRtf.rect.size;
        
        Vector2 vec2 = rtf.localPosition;
        vec2 += rect.min + parentSize * parentRtf.pivot + new Vector2(0, size.y);
        vec2 += new Vector2(0, -parentSize.y);
        
        return vec2;
    }

    ///自己左上角相对父节点左上角的坐标 (x方向: 左为负, 右为正; y方向: 下为负, 上为正)
    public static Vector2 GetLeftTopCornerLPos(RectTransform rtf)
    {
        Rect rect = rtf.rect;
        Vector2 size = rect.size;
        
        var parentRtf = (RectTransform)rtf.parent;
        Vector2 parentSize = parentRtf.rect.size;
        
        Vector2 vec2 = rtf.localPosition;
        vec2 += rect.min + parentSize * parentRtf.pivot + new Vector2(0, size.y);
        vec2 += new Vector2(0, -parentSize.y);
        
        return vec2;
    }

    public static float GetContentScrollDis(RectTransform content)
    {
        Vector2 vec2 = GetLeftTopCornerLPos(content);
        return vec2.y;
    }
    
    ///自己top相对父节点top的距离 (下为正，上为负)
    public static float GetItemNodeTopDis(RectTransform itemNodeRtf)
    {
        Vector2 vec2 = GetLeftTopCornerLPos(itemNodeRtf);
        return -vec2.y;
    }
    
    public static float GetItemNodeLeftDis(RectTransform itemNodeRtf)
    {
        //todo
        return 0;
    }
    
    public static void ContentScrollTo_Vertical(RectTransform content, float scrollDis)
    {
        float y = scrollDis;
        Vector2 leftTopCornerLPos = new Vector2(0, y); //content的左上角相对viewport的左上角的坐标
        
        Rect rect = content.rect;
        Vector2 size = rect.size;
        var parentRtf = (RectTransform) content.parent;
        Vector2 parentSize = parentRtf.rect.size;
        
        Vector2 oldLPos = content.localPosition;
        Vector2 lPos = leftTopCornerLPos - (rect.min + parentSize * parentRtf.pivot + new Vector2(0, size.y) + new Vector2(0, -parentSize.y));
        lPos.x = oldLPos.x;
        content.localPosition = lPos;
    }
    
    public static bool SetStubSize_Vertical(RectTransform stubRtf, float newSize)
    {
        Vector2 sizeDelta = stubRtf.sizeDelta;
        float oldSize = sizeDelta.y;
        if (oldSize <= 0.001f)
        {
            if (newSize > 0.001f)
            {
                stubRtf.gameObject.SetActive(true);
                sizeDelta.y = newSize;
                stubRtf.sizeDelta = sizeDelta;
                Debug.Log($"SetStubSize 111: {stubRtf.name} : {oldSize} -> {newSize}");
                return true;
            }

            return false;
        }
        else
        {
            sizeDelta.y = newSize;
            stubRtf.sizeDelta = sizeDelta;
            if (newSize <= 0.001f)
            {
                stubRtf.gameObject.SetActive(false);
                return false;
            }
            Debug.Log($"SetStubSize 222: {stubRtf.name} : {oldSize} -> {newSize}");
            return true;
        }
    }
    
    public static void SetStubSize_Horizontal(RectTransform stubRtf, float newSize)
    {
        //todo
    }
    
}
